package com.example.gitproject.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.gitproject.Entity.Book;
import com.example.gitproject.dao.BookMapper;
import com.example.gitproject.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

//接口实现类  这个类是用来具体实现接口的方法
@Service
@CacheConfig(cacheNames = "book")   //声明管理book表操作缓存内容  查询  修改  增加
@Transactional
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {
    @Autowired
    private BookMapper bookMapper;

    @Override
    public List<Book> findAll() {
        List<Book> books = bookMapper.selectList(null);  //select * from book
        return books;
    }


    @Override
    @Cacheable(key = "#id")   //缓存参数数据为id的数据内容   第一次进来查询的id是1  缓存保存id是1结果数据       第二次进来查询的id也是1
    public Book findById(Integer id) {
        //条件查询体   提供比较条件  <   >   =    !=    between   like
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);    //select * from book  where  id = {#id}
        return bookMapper.selectOne(wrapper);
    }

//    @Override
//    public int updateById(Integer id, String name) {
//        UpdateWrapper<Book> wrapper = new UpdateWrapper<>();
//        Book book = new Book();
//        book.setName(name);
//        //update book  set  字段名/列名   = name where id = {#id}
//        wrapper.eq("id",id);
//        return bookMapper.updateById(book);
//    }


    @Override
    @CachePut(key = "#id")
    public int updateById(Integer id, String name) {
        UpdateWrapper<Book> wrapper = new UpdateWrapper<>();  //更新的构造器 UpdateWrapper
        wrapper.set("name", name).eq("id", id);  //update  book  set name = {#name}  where id = {#id}
        return bookMapper.update(null, wrapper);
    }


    @Override
    public void delById(Integer id) {
        //1.构造条件器
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        //2.条件器放到delete方法中
        //3.rest风格 DeleteMapping
        //sql  delete  FROM table_name  where  id = 1 ;
        baseMapper.delete(wrapper);
    }


    @Override
    public int insertBook(Book book) {
        //   private Integer id;
        //    private String name;
        //    private String author;
        //    private String press;
        //    private String status;


        Book books = new Book();
        books.setId(book.getId());
        books.setName(book.getName());
        books.setAuthor(book.getAuthor());
        books.setPress(book.getPress());
        books.setStatus(book.getStatus());

        return bookMapper.insert(books);
    }
}
